package com.jstyle.jclife.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;


import com.jstyle.jclife.R;
import com.jstyle.jclife.activity.BaseActivity;
import com.jstyle.jclife.activity.HealthHistoryActivity;
import com.jstyle.jclife.activity.HistoryActivity;
import com.jstyle.jclife.model.HealthData;
import com.jstyle.jclife.model.SleepShowData;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.ArcValue;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.BpValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.ColumnValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SelectedValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.AbstractChartView;
import lecho.lib.hellocharts.view.LineChartView;
import lecho.lib.hellocharts.view.PieChartView;

/**
 * Created by Administrator on 2017/5/19.
 */

public class ChartDataUtil {
    private static final String TAG = "ChartDataUtil";
    static int colorBgChart = Color.parseColor("#012e3d");

//    public static void initChartView(AbstractChartView chart, float top, float bottom, float left, float right) {
//        Viewport viewport = chart.getCurrentViewport();
//        viewport.top = top;
//        viewport.bottom = bottom;
//        viewport.left = left;
//        viewport.right = right;
//        chart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL_IN_ScrollView);
//        chart.setViewportCalculationEnabled(false);
//        chart.setMaximumViewport(viewport);
//        chart.setZoomType(ZoomType.HORIZONTAL);
//        chart.setCurrentViewport(viewport, false);
//        // chart.setZoomEnabled(false);
//    }

    public static void initDataChartView(AbstractChartView chart, float left, float top, float right, float bottom) {
        Viewport viewport = chart.getCurrentViewport();
        viewport.top = top;
        viewport.bottom = bottom;
        viewport.left = left;
        viewport.right = right;
        chart.setScrollEnabled(true);
        chart.setContainerScrollEnabled(true, ContainerScrollType.VERTICAL);
        chart.setViewportCalculationEnabled(false);
        chart.setMaximumViewport(viewport);
        chart.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);
        chart.setCurrentViewport(viewport, false);
    }

    public static void initChartViewDisableZoom(AbstractChartView chart, float left, float top, float right, float bottom) {
        Viewport viewport = chart.getCurrentViewport();
        viewport.top = top;
        viewport.bottom = bottom;
        viewport.left = left;
        viewport.right = right;
        chart.setViewportCalculationEnabled(false);
        chart.setMaximumViewport(viewport);
        chart.setZoomEnabled(false);
        chart.setCurrentViewport(viewport, false);
    }

    public static void initDataSleepChartView(AbstractChartView chart, float left, float top, float right, float bottom, float showRight, float showLeft) {
        Viewport viewport = chart.getCurrentViewport();
        viewport.top = top;
        viewport.bottom = bottom;
        viewport.left = left;
        viewport.right = right;
        chart.setScrollEnabled(true);
        chart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL_IN_ScrollView);
        chart.setViewportCalculationEnabled(false);
        chart.setMaximumViewport(viewport);

        chart.setZoomType(ZoomType.HORIZONTAL);
        viewport.left = showLeft;
        viewport.right = showRight;
        chart.setCurrentViewport(viewport, false);
        //chart.setZoomEnabled(false);
    }

    private static AxisValue getAxisValue(int i) {
        int index = 12 + i / 60 - (12 + i / 60) / 24 * 24;
        return new AxisValue(i, String.format("%1$02d:%2$02d", index, 0).toCharArray());

    }

    public static ColumnChartData getStepDayColumnChart(int[] stepData, float max, Typeface typeface) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        int right = 24;
        for (int i = 0; i < 24; i++) {
            //   if(i%3==0)
            axisesX.add(new AxisValue(i, String.format("%1$02d:%2$02d", i, 0).toCharArray()));
        }
        float count = max / 5;
        axisY.setHasLines(false);
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(1);
        int maxValue = (int) max;
        axisesY.add(new AxisValue(max).setLabel((String.valueOf(maxValue)).toCharArray()));
        axisY.setValues(axisesY).setMaxLabelChars(max >= 10000 ? 5 : 4).setLineColor(Color.parseColor("#FF4081")).setDashPath(true);
        axisY.setValues(axisesY);

        axisY.setHasAxisLines(false).setTypeface(typeface);
        axisX.setValues(axisesX);

        List<Column> columns = new ArrayList<>();
        List<ColumnValue> columnValues = null;
        for (int i = 0; i < right; i++) {
            columnValues = new ArrayList<>();
            int stepTotal = 0;
            int start = i * 60;
            for (int j = 0; j < 60; j++) {//1小时一个柱子
                int step = stepData[j + start];
                stepTotal += step;
            }
            if (stepTotal != 0)
                columnValues.add(new ColumnValue(stepTotal, getStepDayColor(stepTotal)));
            Column column = new Column(columnValues);
            column.setHasRectF(true);
            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        columnChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        columnChartData.setAxisYLeft(axisY.setHasLines(true));
        columnChartData.setFillRatio(1);

//        columnChartData.setBgChartColor(
//                Color.parseColor("#303239"), (int) max);

        return columnChartData;
    }

    public static ColumnChartData getStepHistoryColumnChart(Map<String, Float> maps, String[] dates, String[] axisString, float max, int dataType, Typeface typeface) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        int right = 6;
        for (int i = 0; i < axisString.length; i++) {
            axisesX.add(new AxisValue(i, axisString[i].toCharArray()));
        }
        float count = max;
        axisY.setHasLines(false);
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(1);
        int maxValue = (int) max;
        axisesY.add(new AxisValue(max).setLabel((String.valueOf(maxValue)).toCharArray()));
        axisY.setValues(axisesY).setMaxLabelChars(max >= 10000 ? 5 : 4).setLineColor(Color.parseColor("#FF4081")).setDashPath(true);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX).setTypeface(typeface);
        List<Column> columns = new ArrayList<>();
        List<ColumnValue> columnValues = null;
        for (int i = 0; i < dates.length; i++) {
            Float step = maps.get(dates[i]);
            float stepTotal = step == null ? 0 : step;
            columnValues = new ArrayList<>();
            columnValues.add(new ColumnValue(stepTotal, getStepColor(stepTotal)));
            Column column = new Column(columnValues);
            column.setHasRectF(true);

            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        columnChartData.setFillRatio(0.6f);

        columnChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        columnChartData.setAxisYLeft(axisY);

        columnChartData.setBgChartColor(colorBgChart, max * 1.2f);
        return columnChartData;
    }

    private static float getStepData(float data, int dataType) {
        float result = 0;
        switch (dataType) {
            case HistoryActivity.DATA_DISTANCE:
                result = data * 1000;
                break;
            default:
                result = data;
        }
        return result;
    }

    public static ColumnChartData getStepHistoryMonthColumnChart(Map<String, Float> maps, String[] dates, float max, int dataType) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        for (int i = 0; i < dates.length; i++) {
            int x = Integer.valueOf(dates[i].substring(6));
            axisesX.add(new AxisValue(i, String.valueOf(x).toCharArray()));
        }
        float count = max / 5;
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(1);
        int maxValue = (int) max;
        axisesY.add(new AxisValue(max).setLabel((String.valueOf(maxValue)).toCharArray()));
        axisY.setValues(axisesY).setMaxLabelChars(max >= 10000 ? 5 : 4).setLineColor(Color.parseColor("#FF4081")).setDashPath(true);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX);
        List<Column> columns = new ArrayList<>();
        List<ColumnValue> columnValues = null;
        for (int i = 0; i < dates.length; i++) {
            Float step = maps.get(dates[i]);
            float stepTotal = step == null ? 0 : step;
            columnValues = new ArrayList<>();
            // if(stepTotal>10000)stepTotal=10000;

            columnValues.add(new ColumnValue(stepTotal, getStepColor(stepTotal)));
            Column column = new Column(columnValues);
            column.setHasRectF(true);

            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        columnChartData.setFillRatio(0.6f);
        columnChartData.setAxisXBottom(axisX.setHasAxisLines(false).setTextSize(10));
        columnChartData.setAxisYLeft(axisY);
        columnChartData.setBgChartColor(colorBgChart, max * 1.2f);
        return columnChartData;
    }

    public static ColumnChartData getStepHistoryYearColumnChart(float[] data, float max, int dataType) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            axisesX.add(new AxisValue(i, String.valueOf(i + 1).toCharArray()));
        }
        float count = max / 5;
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(1);
        int maxValue = (int) max;
        axisesY.add(new AxisValue(max).setLabel((String.valueOf(maxValue)).toCharArray()));
        axisY.setValues(axisesY).setMaxLabelChars(max >= 10000 ? 5 : 4).setLineColor(Color.parseColor("#FF4081")).setDashPath(true);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX);
        List<Column> columns = new ArrayList<>();
        List<ColumnValue> columnValues = null;
        for (int i = 0; i < 12; i++) {
            float stepTotal = data[i];
            columnValues = new ArrayList<>();
            // if(stepTotal>10000)stepTotal=10000;

            columnValues.add(new ColumnValue(stepTotal, getStepColor(stepTotal)));
            Column column = new Column(columnValues);
            column.setHasRectF(true);

            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        columnChartData.setFillRatio(0.6f);
        columnChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        columnChartData.setAxisYLeft(axisY);
        columnChartData.setBgChartColor(colorBgChart, max * 1.2f);
        return columnChartData;
    }


    private static int getStepColor(float stepTotal) {
        int stepColor = 0;
        if (stepTotal > 0 && stepTotal <= 2000) {
            stepColor = Color.parseColor("#f37864");
        } else if (stepTotal > 2000 && stepTotal <= 5000) {
            stepColor = Color.parseColor("#fdbb30");
        } else if (stepTotal > 5000 && stepTotal < 10000) {
            stepColor = Color.parseColor("#79cccd");
        } else {
            stepColor = Color.parseColor("#415daa");
        }
        return stepColor;
    }

    private static int getStepDayColor(float stepTotal) {
        int stepColor = 0;
        if (stepTotal > 0 && stepTotal <= 500) {
            stepColor = Color.parseColor("#264cef");
        } else if (stepTotal > 500 && stepTotal <= 2000) {
            stepColor = Color.parseColor("#5fbbf9");
        } else if (stepTotal > 2000 && stepTotal < 5000) {
            stepColor = Color.parseColor("#f8ef6c");
        } else {
            stepColor = Color.parseColor("#e9603c");
        }
        return stepColor;
    }

    private static int getHealthDataColor(Context context, int dataType, int data) {
        int stepColor = 0;
        int[] hrvColor = context.getResources().getIntArray(R.array.color_hrv_array);
        int[] stressColor = context.getResources().getIntArray(R.array.stress_color_arrays);
        if (dataType == HealthHistoryActivity.DATA_HRV) {
            stepColor = hrvColor[ResolveData.getHrvLevel(data)];
        } else if (dataType == HealthHistoryActivity.DATA_PRESSURE) {
            stepColor = stressColor[ResolveData.getStressLevel(data)];
        }
        return stepColor;
    }

    public static int getHealthData(int dataType, HealthData healthData) {
        int stepColor = 0;

        if (dataType == HealthHistoryActivity.DATA_HRV) {
            stepColor = healthData.getHrv();
        } else if (dataType == HealthHistoryActivity.DATA_PRESSURE) {
            stepColor = healthData.getPressure();
        }
        return stepColor;
    }

    /**
     * 睡眠 日
     *
     * @param context
     * @param stepData
     * @return
     */
    public static LineChartData getSleepDayChart(Context context, int[] stepData, List<Integer> sleepTime, Typeface typeface) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();

        List<AxisValue> axisValuesYleft = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        List<Integer> goBedList = new ArrayList<>();
        List<Integer> upBedList = new ArrayList<>();


        for (int i = 0; i < stepData.length; i++) {
            if (i % 24 == 0) {
                String hour = String.format("%02d:00", i / 12 + 12 - i / 144 * 24);
                axisesX.add(new AxisValue(i).setLabel(hour.toCharArray()));
            }
        }
        int[] colors = new int[3];

        colors[0] = context.getResources().getColor(R.color.week_sleep_awake_chart);
        colors[1] = context.getResources().getColor(R.color.week_sleep_light_chart);
        colors[2] = context.getResources().getColor(R.color.week_sleep_deep_chart);

//        for(int i=0;i<3;i++){
//            int y=i*5000;
//            axisesY.add(new AxisValue(y).setLabel((y/1000+"k").toCharArray()));
//        }

        axisValuesYleft.add(new AxisValue(60f, context.getString(R.string.Awake).toCharArray()));
        axisValuesYleft.add(new AxisValue(40f, context.getString(R.string.Light_Sleep).toCharArray()));
        axisValuesYleft.add(new AxisValue(20f, context.getString(R.string.Deep_Sleep).toCharArray()));

        axisY.setValues(axisValuesYleft);
        axisY.setHasLines(false).setTypeface(typeface);
        axisY.setHasAxisLines(false).setCustomColors(colors);
        ;
        axisX.setValues(axisesX);
        List<Line> lines = new ArrayList<>();
        List<PointValue> pointValues = new ArrayList<>();
        for (int i = 0; i < stepData.length; i++) {
            PointValue pointValue = new PointValue();
            pointValue.set(i, stepData[i] > 80 ? 80 : stepData[i]);
            pointValues.add(pointValue);
        }
        Line line = new Line();
        if (sleepTime.size() != 0) {
            for (int i = 0; i < sleepTime.size() / 2; i++) {
                goBedList.add(sleepTime.get(i * 2));
                upBedList.add(sleepTime.get(i * 2 + 1));
            }

            Bitmap bitmapGo = BitmapFactory.decodeResource(context.getResources(), R.drawable.go_bed);
            Bitmap bitmapUp = BitmapFactory.decodeResource(context.getResources(), R.drawable.up_bed);
            line.setGoBedList(goBedList);
            line.setUpBedList(upBedList);
            line.setGoBedBitmap(bitmapGo);
            line.setUpBedBitmap(bitmapUp);
        }

        line.setCubic(true);
        line.setColor(Color.RED).setHasPoints(false).setStrokeWidth(2).setGradient(true).setGradientColor(colors);
        line.setValues(pointValues);
        lines.add(line);
        LineChartData lineChartData = new LineChartData(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY.setMaxLabelChars(6));
        return lineChartData;
    }

    public static LineChartData getSleepHeartDayChart(Context context, int[] stepData, Typeface typeface, int offset, float top) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisValuesYleft = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        //  int right=6;
        for (int i = 0; i < stepData.length; i++) {
            if ((i * offset) % 60 == 0) {
//                Log.i(TAG, "getSleepHeartDayChart: "+i);
                String hour = String.format("%02d:00", (i * offset) / 60 + 12 - (i * offset) / 720 * 24);
                axisesX.add(new AxisValue(i).setLabel(hour.toCharArray()));
            }
        }


        int countValue = (int) ((top - 40) / 2);
        for (int i = 0; i < 3; i++) {
            axisValuesYleft.add(new AxisValue(40 + i * countValue));
        }

        axisY.setValues(axisValuesYleft);
        axisY.setHasLines(false);
        axisY.setHasAxisLines(false);
        ;
        axisX.setValues(axisesX).setScale(2.8f);
        List<Line> lines = new ArrayList<>();
        List<PointValue> pointValues = new ArrayList<>();
        float lastValue = 0;
        int zCount = 0;
        for (int i = 0; i < stepData.length; i++) {
            float hr = stepData[i];
            if (hr == 0 && lastValue != 0) {
                int max = i + 60 >= stepData.length - 1 ? stepData.length - 1 : i + 60;
                in:
                for (int j = i; j < max; j++) {
                    if (stepData[j] != 0) {
                        hr = (float) (stepData[j] + 5 * Math.random());
                        break in;
                    }
                }
            }
            PointValue pointValue = new PointValue();
            if (hr != 0) {
                pointValue.set(i, hr);
                pointValues.add(pointValue);
                lastValue = hr;
            } else {
                lastValue = 0;
                pointValue.set(i, 0);
                pointValues.add(pointValue);
            }

        }
        Line line = new Line();
        line.setCubic(true);
        line.setColor(Color.parseColor("#fc3259")).setHasPoints(false).setStrokeWidth(2);
        line.setValues(pointValues).setFilled(true);

        Bitmap bitmapMax = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart_max);
        line.setMaxBitmap(bitmapMax);
        line.setMinBitmap(bitmapMax);
        lines.add(line);
        LineChartData lineChartData = new LineChartData(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }

    /**
     * 睡眠质量 周
     *
     * @param context
     * @param dateArrays
     * @param dataHashMap
     * @return
     */
    public static ColumnChartData getWeekTopColumnChart(Context context, String[] dateArrays, HashMap<String, SleepShowData> dataHashMap, Typeface typeface) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        int[] colors = new int[4];
        colors[0] = context.getResources().getColor(R.color.week_sleep_bad);
        colors[1] = context.getResources().getColor(R.color.week_sleep_mid);
        colors[2] = context.getResources().getColor(R.color.week_sleep_normal);
        colors[3] = context.getResources().getColor(R.color.week_sleep_good);
        String[] txts = context.getResources().getStringArray(R.array.week_sleep);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axises = new ArrayList<>();
        String[] weekArrays = context.getResources().getStringArray(R.array.week_chart);
        for (int i = 0; i < 7; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(weekArrays[i].toCharArray());
            axises.add(axisValue);

        }
        //  for (int i=0;i<3;i++){
        axisesY.add(new AxisValue(0.8f).setLabel(txts[0].toCharArray()));
        axisesY.add(new AxisValue(1.9f).setLabel(txts[1].toCharArray()));
        axisesY.add(new AxisValue(3.0f).setLabel(txts[2].toCharArray()));
        axisesY.add(new AxisValue(4.1f).setLabel(txts[3].toCharArray()));
        // }
        axisY.setValues(axisesY).setTypeface(typeface).setCustomColors(colors);

//        Bitmap bitmapAxis=BitmapFactory.
//                decodeResource(context.getResources(),R.drawable.bg_axis_week);
        axisX.setValues(axises).setTypeface(typeface);

        List<Column> columns = new ArrayList<>();
        List<ColumnValue> columnValues = new ArrayList<>();
//        columnValues.add(new ColumnValue(0,SleepColor));
//        Column column1=new Column(columnValues);
//        columns.add(column1);
        for (int i = 0; i < 7; i++) {
            columnValues = new ArrayList<>();
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sleep_week_no);
            SleepShowData sleepShowData = dataHashMap.get(dateArrays[i]);
            if (sleepShowData != null) {
                int id = sleepShowData.getSleepLevel();
                columnValues.add(new ColumnValue(id, colors[0]));
                if (id == 4) {
                    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sleep_week_well);
                } else if (id == 3) {
                    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sleep_week_good);
                } else if (id == 2) {
                    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sleep_week_normal);
                } else if (id == 1) {
                    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sleep_week_bad);
                }
            } else {
                columnValues.add(new ColumnValue(0, colors[0]));
            }

            Column column = new Column(columnValues);
            column.setBitmap(bitmap);
            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        columnChartData.setFillRatio(0.2f);
        // columnChartData.setCicleSquare(5);

        axisX.setTextSize(11);
        //  axisX.setAxisBitmap(bitmapAxis);
        columnChartData.setAxisXBottom(axisX.setHasAxisLines(false).setTextColor(context.getResources().getColor(R.color.style_text_color)));
        columnChartData.setAxisYLeft(axisY.setHasAxisLines(false).setMaxLabelChars(8));
        return columnChartData;
    }

    /**
     * 睡眠 周
     *
     * @param context
     * @param dateArrays
     * @param dataHashMap
     * @return
     */
    public static ColumnChartData getWeekSleepColumnChart(Context context, String[] dateArrays, HashMap<String, SleepShowData> dataHashMap, Typeface typeface) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10).setLineColor(context.getResources().getColor(R.color.style_text_color));
        int[] colors = new int[3];
        colors[0] = context.getResources().getColor(R.color.week_sleep_awake);
        colors[1] = context.getResources().getColor(R.color.week_sleep_light);
        colors[2] = context.getResources().getColor(R.color.week_sleep_deep);
        String[] weekArrays = context.getResources().getStringArray(R.array.week_chart);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axises = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(weekArrays[i].toCharArray());
            axises.add(axisValue);
        }
        for (int i = 0; i < 15; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(String.valueOf(i).toCharArray());
            axisesY.add(axisValue);
        }
        axisY.setValues(axisesY).setTypeface(typeface);
        axisX.setValues(axises).setTypeface(typeface);
        List<Column> columns = new ArrayList<>();
        List<ColumnValue> columnValues = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            columnValues = new ArrayList<>();
            SleepShowData sleepShowData = dataHashMap.get(dateArrays[i]);
            if (sleepShowData != null) {
                columnValues.add(new ColumnValue(sleepShowData.getAwakeTime(), colors[0]));
                columnValues.add(new ColumnValue(sleepShowData.getDeepTime(), colors[1]));
                columnValues.add(new ColumnValue(sleepShowData.getLightTime(), colors[2]));

            } else {
                columnValues.add(new ColumnValue(0, colors[0]));
                columnValues.add(new ColumnValue(0, colors[1]));
                columnValues.add(new ColumnValue(0, colors[2]));

            }
            Column column = new Column(columnValues);
            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        axisX.setTextSize(11);
        columnChartData.setAxisXBottom(axisX.setHasAxisLines(false).setTextColor(context.getResources().getColor(R.color.style_text_color)));
        columnChartData.setAxisYLeft(axisY.setHasLines(true));
        return columnChartData;
    }

    /**
     * 睡眠 月
     *
     * @param context
     * @param dateArrays
     * @param dataHashMap
     * @return
     */
    public static ColumnChartData getMonthColumnChart(Context context, String[] dateArrays, HashMap<String, SleepShowData> dataHashMap) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axises = new ArrayList<>();
        for (int i = 0; i < dateArrays.length; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(String.valueOf(i + 1).toCharArray());
            axises.add(axisValue);
        }
//        for (int i = 0; i < 11; i++) {
//            AxisValue axisValue = new AxisValue(i).setLabel(String.valueOf(i).toCharArray());
//            axisesY.add(axisValue);
//        }
        int[] colors = new int[3];
        colors[0] = context.getResources().getColor(R.color.week_sleep_awake);
        colors[1] = context.getResources().getColor(R.color.week_sleep_light);
        colors[2] = context.getResources().getColor(R.color.week_sleep_deep);
        axisX.setValues(axises);

        List<Column> columns = new ArrayList<>();
        List<ColumnValue> columnValues = new ArrayList<>();

        for (int i = 0; i < dateArrays.length; i++) {
            columnValues = new ArrayList<>();
            SleepShowData sleepShowData = dataHashMap.get(dateArrays[i]);
            if (sleepShowData != null) {
                columnValues.add(new ColumnValue(sleepShowData.getAwakeTime(), colors[0]));
                columnValues.add(new ColumnValue(sleepShowData.getLightTime(), colors[1]));
                columnValues.add(new ColumnValue(sleepShowData.getDeepTime(), colors[2]));
            } else {
                columnValues.add(new ColumnValue(0, colors[0]));
            }

            Column column = new Column(columnValues);
            columns.add(column);

        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        columnChartData.setStacked(true);
        axisX.setTextSize(11);
        columnChartData.setAxisXBottom(axisX.setHasAxisLines(false).setTextColor(context.getResources().getColor(R.color.style_text_color)));
        columnChartData.setAxisYLeft(axisY.setHasLines(true).setLineColor(context.getResources().getColor(R.color.style_text_color)));
        return columnChartData;
    }

    /**
     * 睡眠心率 月
     *
     * @param context
     * @param dateArrays
     * @param
     * @return
     */
    public static LineChartData getSleepMonthHeartChart(Context context, String[] dateArrays, HashMap<String, Float> heartRateMap) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisValuesYleft = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        //  int right=6;
        for (int i = 0; i < dateArrays.length; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(String.valueOf(i + 1).toCharArray());
            axisesX.add(axisValue);
        }

        for (int i = 0; i < 11; i++) {
            int y = i * 10;
            axisValuesYleft.add(new AxisValue(y).setLabel(String.valueOf(y).toCharArray()));
        }
        axisY.setValues(axisValuesYleft);
        axisY.setHasLines(false);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX);
        List<Line> lines = new ArrayList<>();
        List<PointValue> pointValues = new ArrayList<>();
        for (int i = 0; i < dateArrays.length; i++) {
            Float value = heartRateMap.get(dateArrays[i]);
            PointValue pointValue = new PointValue();
            if (value != null) {
                pointValue.set(i, value);
            } else {
                pointValue.set(i, 0);
            }
            pointValues.add(pointValue);
        }
        Line line = new Line();
        line.setCubic(true);
        line.setColor(Color.parseColor("#d51f2f")).setHasPoints(false).setStrokeWidth(2);
        line.setValues(pointValues);
        lines.add(line);
        LineChartData lineChartData = new LineChartData(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }

    /**
     * 睡眠心率 周
     *
     * @param context
     * @param dateArrays
     * @param
     * @return
     */
    public static LineChartData getSleepWeekHeartChart(Context context, String[] dateArrays, HashMap<String, Float> dateFloat, Typeface typeface) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisValuesYleft = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        String[] weekArrays = context.getResources().getStringArray(R.array.week_chart);

        for (int i = 0; i < weekArrays.length; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(weekArrays[i].toCharArray());
            axisesX.add(axisValue);
        }

        for (int i = 0; i < 11; i++) {
            int y = i * 10;
            axisValuesYleft.add(new AxisValue(y).setLabel(String.valueOf(y).toCharArray()));
        }
        axisY.setValues(axisValuesYleft);
        axisY.setHasLines(false);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX).setTypeface(typeface);
        List<Line> lines = new ArrayList<>();
        List<PointValue> pointValues = new ArrayList<>();
        for (int i = 0; i < weekArrays.length; i++) {
            Float value = dateFloat.get(dateArrays[i]);
            PointValue pointValue = new PointValue();
            if (value != null) {
                pointValue.set(i, value);
            } else {
                pointValue.set(i, 0);
            }
            pointValues.add(pointValue);
        }
        Line line = new Line();
        line.setCubic(true);
        line.setColor(Color.parseColor("#d51f2f")).setHasPoints(false).setStrokeWidth(2);
        line.setValues(pointValues);
        lines.add(line);
        LineChartData lineChartData = new LineChartData(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }

    public static LineChartData getWeekHeartHistoryChart(Context context, String[] dateArrays,
                                                         HashMap<String, Integer> maxMap, Typeface typeface, float top) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisValuesYleft = new ArrayList<>();

        int countValue = (int) ((top - 40) / 2);
        for (int i = 0; i < 3; i++) {
            axisValuesYleft.add(new AxisValue(40 + i * countValue));
        }
        List<AxisValue> axisesX = new ArrayList<>();
        String[] weekArrays = context.getResources().getStringArray(R.array.week_chart);

        for (int i = 0; i < weekArrays.length; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(weekArrays[i].toCharArray());
            axisesX.add(axisValue);
        }
        List<PointValue> lineMaxValues = new ArrayList<>();


        axisY.setHasLines(false);
        axisY.setHasAxisLines(false).setValues(axisValuesYleft);
        axisX.setValues(axisesX).setTypeface(typeface);
        List<Line> lines = new ArrayList<>();
        for (int i = 0; i < weekArrays.length; i++) {
            Integer valueMax = maxMap.get(dateArrays[i]);
            lineMaxValues.add(new PointValue(i, valueMax == null ? 0 : valueMax));
        }
        Line lineMax = new Line();
        lineMax.setCubic(true);
        lineMax.setColor(Color.parseColor("#fc3259")).setHasPoints(false).setStrokeWidth(2);
        lineMax.setValues(lineMaxValues).setFilled(true);
        Bitmap bitmapMax = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart_max);
        lineMax.setMaxBitmap(bitmapMax);
        lineMax.setMinBitmap(bitmapMax);


        lines.add(lineMax);
        LineChartData lineChartData = new LineChartData(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }

    public static LineChartData getMonthHeartHistoryChart(Context context, String[] dateArrays,
                                                          HashMap<String, Integer> maxMap, float top) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisesX = new ArrayList<>();
        List<AxisValue> axisValuesYleft = new ArrayList<>();

        int countValue = (int) ((top - 40) / 2);
        for (int i = 0; i < 3; i++) {
            axisValuesYleft.add(new AxisValue(40 + i * countValue));
        }
        for (int i = 0; i < dateArrays.length; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(String.valueOf(i + 1).toCharArray());
            axisesX.add(axisValue);
        }
        List<PointValue> lineMaxValues = new ArrayList<>();


        axisY.setHasLines(false);
        axisY.setHasAxisLines(false).setValues(axisValuesYleft);
        axisX.setValues(axisesX);
        List<Line> lines = new ArrayList<>();
        for (int i = 0; i < dateArrays.length; i++) {
            Integer valueMax = maxMap.get(dateArrays[i]);
            lineMaxValues.add(new PointValue(i, valueMax == null ? 0 : valueMax));
        }
        Line lineMax = new Line();
        lineMax.setCubic(true);
        lineMax.setColor(Color.parseColor("#fc3259")).setHasPoints(false).setStrokeWidth(2);

        lineMax.setValues(lineMaxValues).setFilled(true);
        Bitmap bitmapMax = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart_max);
        lineMax.setMaxBitmap(bitmapMax);
        lineMax.setMinBitmap(bitmapMax);
        lines.add(lineMax);
        LineChartData lineChartData = new LineChartData(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }

    public static LineChartData getYearHeartHistoryChart(Context context, HashMap<Integer, Integer> maxMap, float top) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);

        List<AxisValue> axisesX = new ArrayList<>();

        List<AxisValue> axisValuesYleft = new ArrayList<>();
        int countValue = (int) ((top - 40) / 2);
        for (int i = 0; i < 3; i++) {
            axisValuesYleft.add(new AxisValue(40 + i * countValue));
        }
        for (int i = 0; i < 12; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(String.valueOf(i + 1).toCharArray());
            axisesX.add(axisValue);
        }
        List<PointValue> lineMaxValues = new ArrayList<>();


        axisY.setHasLines(false);
        axisY.setHasAxisLines(false).setValues(axisValuesYleft);
        axisX.setValues(axisesX);
        List<Line> lines = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            Integer valueMax = maxMap.get(i + 1);

            lineMaxValues.add(new PointValue(i, valueMax == null ? 0 : valueMax));

        }
        Line lineMax = new Line();
        lineMax.setCubic(true);
        lineMax.setColor(Color.parseColor("#fc3259")).setHasPoints(false).setStrokeWidth(2);

        lineMax.setValues(lineMaxValues).setFilled(true);
        Bitmap bitmapMax = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart_max);
        lineMax.setMaxBitmap(bitmapMax);
        lineMax.setMinBitmap(bitmapMax);
        lines.add(lineMax);
        LineChartData lineChartData = new LineChartData(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }

    /**运动历史步数
     * @param context
     * @param time
     * @param data 步数集合，如果都为0的话画图会给默认值2（只同步运动数据没有同步步数数据导致）
     * @return
     */
    public static ColumnChartData getTrackStepColumnChart(Context context, float time, List<Integer> data) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axises = new ArrayList<>();
        int size = (int) (time / 60 + 1);
        for (int i = 0; i < size; i++) {
            AxisValue axisValue = new AxisValue(i).setLabel(String.valueOf(i + 1).toCharArray());
            axises.add(axisValue);
        }
        axisX.setValues(axises);
        List<Column> columns = new ArrayList<>();
        List<ColumnValue> columnValues = new ArrayList<>();
        int color = context.getResources().getColor(R.color.exercise_step_color);
        int max = 0;
        int maxIndex = 0;
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i) > max) {
                max = data.get(i);
                maxIndex = i;
            }
            columnValues = new ArrayList<>();
            columnValues.add(new ColumnValue(data.get(i) == 0 ? 2 : data.get(i), color).setHasGradient(true));
            Column column = new Column(columnValues);
            columns.add(column);
        }
        Bitmap bitmapMax = BitmapFactory.decodeResource(
                context.getResources(), R.drawable.max_step);

        ColumnChartData columnChartData = new ColumnChartData(columns);

        axisX.setTextSize(11);
        columnChartData.setMaxIndex(maxIndex);
        columnChartData.setValueLabelTextSize(10);
        columnChartData.setMaxStepBitmap(bitmapMax);
        columnChartData.setAxisXBottom(axisX.setHasAxisLines(false).setTextColor(context.getResources().getColor(R.color.style_text_color)));
        columnChartData.setAxisYLeft(axisY.setHasAxisLines(false));
        return columnChartData;
    }

    public static LineChartData getTrackHistoryHeartChart(Context context, List<PointValue> pointValues) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisValuesYleft = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            axisValuesYleft.add(new AxisValue(30 * (i + 1)));
        }
        List<AxisValue> axisesX = new ArrayList<>();
        for (int i = 0; i < pointValues.size(); i++) {
            axisesX.add(new AxisValue(i).setLabel(String.valueOf(i + 1).toCharArray()));
        }
        axisY.setValues(axisValuesYleft);
        axisY.setHasLines(false);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX);
        List<Line> lines = new ArrayList<>();
        Line line = new Line();
        line.setCubic(true);
        Bitmap bitmapMax = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart_max);
        line.setMaxBitmap(bitmapMax);
        line.setMinBitmap(bitmapMax);
        line.setColor(Color.parseColor("#aaFF0000")).setHasPoints(false).setStrokeWidth(1);
        line.setValues(pointValues);
        line.setFilled(true);
        lines.add(line);
        LineChartData lineChartData = new LineChartData(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }

    public static LineChartData getHistoryHeartDayChart(Context context, int[] maxHeartArray,
                                                        Typeface typeface, int offsetCount, float max) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        List<AxisValue> axisValuesYleft = new ArrayList<>();
        int countValue = (int) ((max - 30) / 2);
        for (int i = 0; i < 3; i++) {
            axisValuesYleft.add(new AxisValue(30 + i * countValue));
        }
        List<AxisValue> axisesX = new ArrayList<>();
        for (int i = 0; i < maxHeartArray.length; i++) {
            if (i % 60 == 0)
                axisesX.add(new AxisValue(i, String.format("%1$02d:%2$02d", i  / 60, 0).toCharArray()));
        }
        List<Line> lines = new ArrayList<>();
        List<PointValue> lineMaxValues = new ArrayList<>();
        int length= maxHeartArray.length;
       out: for (int i = 0; i < length; i++) {
            int data = maxHeartArray[i];
            if (data != 0) {
               // Log.i(TAG, "getHistoryHeartDayChart: "+i+" "+data);
                lineMaxValues.add(new PointValue(i, data));

            }
        }
        axisY.setHasLines(false).setValues(axisValuesYleft);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX).setTypeface(typeface);
        Line lineMax = new Line();
        lineMax.setCubic(true);
        lineMax.setStrokeWidth(1);
        lineMax.setColor(Color.parseColor("#fc3259")).setHasPoints(false).setStrokeWidth(2);
        lineMax.setValues(lineMaxValues).setFilled(true);
        lineMax.setMaxOffset(60);
        Bitmap bitmapMax = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart_max);
        lineMax.setMaxBitmap(bitmapMax);
        lineMax.setMinBitmap(bitmapMax);


        lines.add(lineMax);
        LineChartData lineChartData = new LineChartData(lines);
        //axisX.setDrawSize(24);
        lineChartData.setAxisXBottom(axisX.setHasLines(true));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }

    public static LineChartData getHealthDayLineChart(int dataType, List<HealthData> healthDataList,int top) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        axisX.setTextSize(8);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        int index=top/2;
        for(int i=0;i<3;i++){
            axisesY.add(new AxisValue(i*index,String.valueOf((i*index)).toCharArray()));
        }

        axisY.setHasLines(false);
        axisY.setValues(axisesY);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX);
        List<PointValue> list = new ArrayList<>();
        for (int i = 0; i < healthDataList.size(); i++) {
            HealthData healthData = healthDataList.get(i);
            String time=healthData.getTime().split(" ")[1];
            int dataValue= getHealthData(dataType, healthData);
            list.add(new PointValue(i,dataValue));
            axisesX.add(new AxisValue(i, time.substring(0,5).toCharArray()));
        }
        Line line = new Line();
        line.setColor(Color.parseColor("#fcca03"));
        line.setStrokeWidth(2);
        line.setPointRadius(5);
        line.setShape(ValueShape.JSTYLE_circle);
        line.setHasPoints(true).setHasLabelsOnlyForSelected(true);
        line.setPointColor(Color.parseColor("#e9603c"));
        line.setValues(list);
        LineChartData lineChartData = new LineChartData();
        List<Line> lines = new ArrayList<>();
        lines.add(line);
        lineChartData.setLines(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }
    public static LineChartData getHealthWeekLineChart(Context context,  HashMap<String, Integer> weekMap,int top,String[]weekDates) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        axisX.setTextSize(8);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        int index=top/2;
        for(int i=0;i<3;i++){
            axisesY.add(new AxisValue(i*index,String.valueOf((i*index)).toCharArray()));
        }
        String[] weekArrays = context.getResources().getStringArray(R.array.week_chart);
        if(weekDates.length>7){
            for (int i = 0; i < weekDates.length; i++) {
                int x = Integer.valueOf(weekDates[i].substring(6));
                axisesX.add(new AxisValue(i, String.valueOf(x).toCharArray()));
            }
        }else {
            for (int i = 0; i < 7; i++) {
                AxisValue axisValue = new AxisValue(i).setLabel(weekArrays[i].toCharArray());
                axisesX.add(axisValue);
            }
        }


        axisY.setHasLines(false);
        axisY.setValues(axisesY);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX);
        List<PointValue> list = new ArrayList<>();

        for(int i = 0; i < weekDates.length; i++){

            Integer dataValue=weekMap.get(weekDates[i]);
            if(dataValue!=null)
            list.add(new PointValue(i,dataValue));

        }
//        for (int i = 0; i < healthDataList.size(); i++) {
//            HealthData healthData = healthDataList.get(i);
//           // String time=healthData.getTime().split(" ")[1];
//            int dataValue= getHealthData(dataType, healthData);
//            list.add(new PointValue(i,dataValue));
//          //  axisesX.add(new AxisValue(i, time.substring(0,5).toCharArray()));
//        }
        Line line = new Line();
        line.setColor(Color.parseColor("#fcca03"));
        line.setStrokeWidth(2);
        line.setPointRadius(5);
        line.setShape(ValueShape.JSTYLE_circle);
        line.setHasPoints(true).setHasLabelsOnlyForSelected(true);
        line.setPointColor(Color.parseColor("#e9603c"));
        line.setValues(list);
        LineChartData lineChartData = new LineChartData();
        List<Line> lines = new ArrayList<>();
        lines.add(line);
        lineChartData.setLines(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }
    public static LineChartData getHealthYearLineChart( float[]yearData,float top) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        axisY.setTextSize(10);
        axisX.setTextSize(8);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        int index= (int) (top/2);
        for(int i=0;i<3;i++){
            axisesY.add(new AxisValue(i*index,String.valueOf((i*index)).toCharArray()));
        }

            for (int i = 0; i < 12; i++) {
                AxisValue axisValue = new AxisValue(i).setLabel(String.valueOf(i+1).toCharArray());
                axisesX.add(axisValue);
            }



        axisY.setHasLines(false);
        axisY.setValues(axisesY);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX);
        List<PointValue> list = new ArrayList<>();

        for(int i = 0; i < yearData.length; i++){

            float dataValue=yearData[i];
            if(dataValue!=0)
            list.add(new PointValue(i,dataValue));

        }
//        for (int i = 0; i < healthDataList.size(); i++) {
//            HealthData healthData = healthDataList.get(i);
//           // String time=healthData.getTime().split(" ")[1];
//            int dataValue= getHealthData(dataType, healthData);
//            list.add(new PointValue(i,dataValue));
//          //  axisesX.add(new AxisValue(i, time.substring(0,5).toCharArray()));
//        }
        Line line = new Line();
        line.setColor(Color.parseColor("#fcca03"));
        line.setStrokeWidth(2);
        line.setPointRadius(5);
        line.setShape(ValueShape.JSTYLE_circle);
        line.setHasPoints(true).setHasLabelsOnlyForSelected(true);
        line.setPointColor(Color.parseColor("#e9603c"));
        line.setValues(list);
        LineChartData lineChartData = new LineChartData();
        List<Line> lines = new ArrayList<>();
        lines.add(line);
        lineChartData.setLines(lines);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartData.setAxisYLeft(axisY);
        return lineChartData;
    }

    public static ColumnChartData getHealthDayBloodColumnChart(Context context, List<BpValue> listBpValue, float max) {
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        Axis axisYRight = new Axis();
        Bitmap bitmapHIGH = BitmapFactory.decodeResource(context.getResources(), R.drawable.high_tips);
        Bitmap bitmapLow = BitmapFactory.decodeResource(context.getResources(), R.drawable.low_tips);
        List<AxisValue> axisValuesRight = new ArrayList<>();
        axisValuesRight.add(new AxisValue(60).setLabel("".toCharArray()));
        axisValuesRight.add(new AxisValue(139).setLabel("".toCharArray()));
        axisYRight.setValues(axisValuesRight).setDashPath(true);
        axisY.setTextSize(10);
        List<AxisValue> axisesY = new ArrayList<>();
        List<AxisValue> axisesX = new ArrayList<>();
        int right = 24;
        for (int i = 0; i < listBpValue.size(); i++) {
            String time = listBpValue.get(i).getDate();
            axisesX.add(new AxisValue(i, time.substring(0, 5).toCharArray()));
        }
        for (int i = 0; i < 7; i++) {
            axisesY.add(new AxisValue(i * 30, String.valueOf(i * 30).toCharArray()));
        }
        axisY.setValues(axisesY);
        axisY.setHasLines(false);
        axisY.setHasAxisLines(false);
        axisX.setValues(axisesX);
        List<Column> columns = new ArrayList<>();
        List<ColumnValue> columnValues = null;
        List<BpValue> bpValueList = new ArrayList<>();
        for (int i = 0; i < listBpValue.size(); i++) {
            columnValues = new ArrayList<>();
            BpValue bpValue = listBpValue.get(i);
            if (bpValue != null) {
                bpValueList.add(bpValue);
            }
//
//            if (stepTotal != 0)
//                columnValues.add(new ColumnValue(stepTotal, getStepDayColor(stepTotal)));
            Column column = new Column(columnValues);
            column.setHasRectF(true);
            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);

        columnChartData.setAxisXBottom(axisX.setHasAxisLines(false).setTextSize(8));
        columnChartData.setAxisYLeft(axisY);
        columnChartData.setAxisYRight(axisYRight.setMaxLabelChars(0));
        columnChartData.setBpValues(bpValueList);
        columnChartData.setHighBitmap(bitmapHIGH);
        columnChartData.setLowBitmap(bitmapLow);
        columnChartData.setFillRatio(0.5f);
        columnChartData.setBgChartColor(
                Color.parseColor("#66013499"), (int) max);
        return columnChartData;
    }


    public static void setHeartPieChartView(PieChartView pieChartView, int[] colors, String result, int level, int width) {
        List<ArcValue> arcValues = new ArrayList<>();
        for (int i = 0; i < colors.length; i++) {
            arcValues.add(new ArcValue(1, colors[i]).setArcSpacing(2));
        }
        PieChartData pieChartData = new PieChartData(arcValues);
        pieChartData.setHasCenterCircle(true);
        pieChartData.setCenterText1Color(level == -1 ? Color.WHITE : colors[level]);
        pieChartData.setCenterText1(result);
        pieChartData.setCenterText1FontSize(12);
        pieChartData.setCenterCircleWidth(width);
        pieChartData.setCenterCircleColor(Color.parseColor("#181c2a"));
        pieChartView.setPieChartData(pieChartData);
        if (level != -1) pieChartView.selectValue(new SelectedValue(level, 0, 0));
        // pieChartView.set;
    }

    public static LineChartData getEcgLineChartData(Context context, List<Double> queue, int color, int second, int index) {
        LineChartData lineChartData = new LineChartData();
        List<Line> listLines = new ArrayList<>();
        Line line = new Line();

        List<PointValue> listPoint = new ArrayList<>();
        List<AxisValue> axisValues = new ArrayList<>();
        Axis axisX = new Axis();
        for (int i = 0; i < second; i++) {
            axisValues.add(new AxisValue(i * 512).setLabel(String.valueOf(i + "s").toCharArray()));
        }
        axisX.setValues(axisValues);
        int size = queue.size();

//        Double[]drawData=new Double[size];
//        if(size!=0){
//            drawData= queue.toArray(drawData);
//            drawData=ResolveData.getSleepQData(drawData);
//        }

        int maxSize = (second - 1) * 512;

        for (int i = 0; i < maxSize; i++) {
            if (i + index >= size) break;
           // Double data = drawData[i + index];
            Double data = queue.get(i + index);
            PointValue pointValue = new PointValue();
            if (data != null) {
                double v=data;
                pointValue.set(i, (float) v);
                listPoint.add(pointValue);
            }

        }
//        for (int i = 0; i < size; i++) {
//            if(queue.get(i)!=null){
//                int data = queue.get(i);
//
//            }
//
//        }
        line.setValues(listPoint);
        line.setColor(color);
        line.setCubic(true);
        line.setStrokeWidth(1);
        line.setHasPoints(false);
        listLines.add(line);
        lineChartData.setLines(listLines);
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_ecg_h);
        lineChartData.setBitmap(bitmap);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        return lineChartData;
    }

    public static LineChartData getEcgReportLineChartData(Context context, List<Double> value, int color) {
        LineChartData lineChartData = new LineChartData();
        List<Line> listLines = new ArrayList<>();
        Line line = new Line();
        List<PointValue> listPoint = new ArrayList<>();
        List<AxisValue> axisValues = new ArrayList<>();
        Axis axisX = new Axis();
        int xSize = value.size() / 512;
        for (int i = 0; i < xSize; i++) {
            axisValues.add(new AxisValue(i * 512).setLabel(String.valueOf(i + "s").toCharArray()));
        }
        axisX.setValues(axisValues);
        for (int i = 0; i < value.size(); i++) {
            double data = value.get(i);
            PointValue pointValue = new PointValue();
            pointValue.set(i, (float) data);
            listPoint.add(pointValue);
        }
        line.setValues(listPoint);
        line.setColor(color);
        line.setCubic(true);
        line.setStrokeWidth(2);
        line.setHasPoints(false);
        listLines.add(line);
        lineChartData.setLines(listLines);
//        Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),R.drawable.bg_ecg_h);
//        lineChartData.setBitmap(bitmap);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        return lineChartData;
    }

    public static LineChartData getPpgLineChartData(Context context, List<Float> queue, int color) {
        LineChartData lineChartData = new LineChartData();
        List<Line> listLines = new ArrayList<>();
        Line line = new Line();
        List<PointValue> listPoint = new ArrayList<>();
        List<AxisValue> axisValues = new ArrayList<>();
        Axis axisX = new Axis();
        int xSize = queue.size() / 200;
        for (int i = 0; i < xSize; i++) {
            axisValues.add(new AxisValue(i * 200).setLabel(String.valueOf(i + "s").toCharArray()));
        }
        axisX.setValues(axisValues);
        for (int i = 0; i < queue.size(); i++) {
            float data = queue.get(i);
            PointValue pointValue = new PointValue();
            pointValue.set(i, data);
            listPoint.add(pointValue);
        }

        line.setValues(listPoint);
        line.setColor(color);
        line.setCubic(true);
        line.setStrokeWidth(1);
        line.setHasPoints(false);
        listLines.add(line);
        lineChartData.setLines(listLines);
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_ecg_h);
        lineChartData.setBitmap(bitmap);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        return lineChartData;
    }
    public static void getPpgLineChartData(LineChartView lineChartView,Context context, List<Float> queue, int color) {
        LineChartData lineChartData = new LineChartData();
        List<Line> listLines = new ArrayList<>();
        Line line = new Line();
        List<PointValue> listPoint = new ArrayList<>();
        List<AxisValue> axisValues = new ArrayList<>();
        Axis axisX = new Axis();
        int xSize = queue.size() / 200;
        for (int i = 0; i < xSize; i++) {
            axisValues.add(new AxisValue(i * 200).setLabel(String.valueOf(i + "s").toCharArray()));
        }
        axisX.setValues(axisValues);
        double maxPPG = 0;
        double minPPg = 33000;
        for (int i = 0; i < queue.size(); i++) {
            float data = queue.get(i);
            PointValue pointValue = new PointValue();
            pointValue.set(i, data);
            listPoint.add(pointValue);
            maxPPG = Math.max(maxPPG, data);
            minPPg = Math.min(minPPg, data);
        }
        initDataChartView(lineChartView, 0, (float) (maxPPG*1.2f), queue.size(), (float) (minPPg*1.2f));

        line.setValues(listPoint);
        line.setColor(color);
        line.setCubic(true);
        line.setStrokeWidth(1);
        line.setHasPoints(false);
        listLines.add(line);
        lineChartData.setLines(listLines);
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_ecg_h);
        lineChartData.setBitmap(bitmap);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        lineChartView.setLineChartData(lineChartData);
       // return lineChartData;
    }

    static double[] tmp_x = new double[5];
    static double[] tmp_y = new double[5];
    static double IIR8_B[] = {
            2.740724471807023,0,-5.481448943614046,0, 2.740724471807023
    };
    static double IIR8_A[] = {
            1.0000,-3.966563683749257,5.903228691653986 ,-3.906734871440351,0.970070822869830
    };

    public synchronized static float getPPgData(int value) {
        if (value >= 32768) value = value - 65536;
        for (int i = 2; i > 0; i--) {
            tmp_x[i] = tmp_x[i - 1];
            tmp_y[i] = tmp_y[i - 1];
        }
        tmp_x[0] = value;
        tmp_y[0] = IIR8_B[0] * tmp_x[0] +
                IIR8_B[1] * tmp_x[1] +
                IIR8_B[2] * tmp_x[2] -
                IIR8_A[1] * tmp_y[1] -
                IIR8_A[2] * tmp_y[2];
        // Log.i(TAG, "getPPgData: "+tmp_y[0]);
        if (tmp_y[0] > 199) tmp_y[0] = 199;
        if (tmp_y[0] < -199) tmp_y[0] = -199;
        return (float) tmp_y[0];
    }

    public synchronized static double[] getPPgData(List<Float>value) {
        Float[]source=value.toArray(new Float[value.size()]);
        for(int i=4;i<source.length;i++){
            source[i]=(source[i]+source[i-1]+source[i-2]+source[i-3]+source[i-4])/5;
        }
        if(source.length<4)return new double[4];
        Float[]avgSource=new Float[source.length-4];
        System.arraycopy(source,4,avgSource,0,avgSource.length);
        double[]s_proc=new double[avgSource.length];
        for(int i=4;i<s_proc.length;i++){
            s_proc[i]=0.0001*IIR8_B[0] * avgSource[i] +
                    0.0001*IIR8_B[1] * avgSource[i-1] +
                    0.0001*IIR8_B[2] * avgSource[i-2]+
                    0.0001*IIR8_B[3] * avgSource[i-3] +
                    0.0001*IIR8_B[4] * avgSource[i-4] -
                    IIR8_A[1] * s_proc[i-1] -
                    IIR8_A[2] * s_proc[i-2]-
                    IIR8_A[3] * s_proc[i-3] -
                    IIR8_A[4] * s_proc[i-4];
        }
        double[]result=new double[s_proc.length-4];
        System.arraycopy(s_proc,4,result,0,result.length);

        return s_proc;
    }
    public static long showMills;
    public static LineChartData getPpgChartData(LineChartView lineChartView,Context context, List<Float> queue, int color) {
        LineChartData lineChartData = new LineChartData();
        List<Line> listLines = new ArrayList<>();
        Line line = new Line();
        List<PointValue> listPoint = new ArrayList<>();
        List<AxisValue> axisValues = new ArrayList<>();
        Axis axisX = new Axis();
        int xSize = queue.size() / 200;
        for (int i = 0; i < xSize; i++) {
            axisValues.add(new AxisValue(i * 200).setLabel(String.valueOf(i + "s").toCharArray()));
        }
        axisX.setValues(axisValues);
        double[]result=getPPgData(queue);
        float maxValue=0;
        float minValue=10000;
        for (int i = 0; i < result.length; i++) {
            float data = (float) result[i];
            maxValue=Math.max(maxValue,data);
            minValue=Math.min(minValue,data);
            PointValue pointValue = new PointValue();
            pointValue.set(i, data);
            listPoint.add(pointValue);
        }
        float top=0;
        float bottom=0;
        if(maxValue!=0){
            Log.i(TAG, "getPpgChartData: "+maxValue+" "+minValue);
            if(maxValue-minValue<40){
                float offset=(50-(maxValue-minValue))/2;
                top=maxValue+offset;
                bottom=minValue-offset;
                if(maxValue-minValue<10){
                    Log.i(TAG, "getPpgChartData: 按压太紧或者漏光");
                    if(System.currentTimeMillis()-showMills>4000) {
                        ScreenUtils.showSetSuccessFul(lineChartView,context.getResources().getString(R.string.ppgTips));
                        showMills=System.currentTimeMillis();
                    }

                }
            }else{
                top=maxValue*1.25f;
                bottom=minValue*1.25f;
            }
        }
        Log.i(TAG, "getPpgChartData: top "+top+" "+bottom);
        initDataChartView(lineChartView, 0, top, queue.size(), bottom);

        line.setValues(listPoint);
        line.setColor(color);
        line.setCubic(true);
        line.setStrokeWidth(1);
        line.setHasPoints(false);
        listLines.add(line);
        lineChartData.setLines(listLines);
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_ecg_h);
        lineChartData.setBitmap(bitmap);
        lineChartData.setAxisXBottom(axisX.setHasAxisLines(false));
        return lineChartData;
    }

}
